/*!
	* @Title: 订单管理
	* @author 		naluv125@hotmail.com
	* @date 			2013-7-29 下午12:59:34   
	* @version    	V1.0
 */

Ext.define('MyDesktop.Tord', {
    extend: 'Ext.ux.desktop.Module',

    requires: [
        'Bleext.abstract.GridForm'
    ],

    id:'ord-win',

    init : function(){
        this.launcher = {
            text: '订单管理',
            iconCls:'img_ord'
        };
    },

    createWindow : function(){
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('ord-win');
        if(!win){
        	var ord = this.buildDistModule();
            win = desktop.createWindow({
                id: 'ord-win',
                title:'订单管理',
                width:1080,
                height:610,
                iconCls: 'img_ord',
                animCollapse:false,
                border:false,
                constrainHeader:true, //窗口顶部不能超过浏览器外界
                layout: 'fit',
                items: [ord]
            });
        }
        return win;
    },
    
    buildDistModule:function(){
    		var ordInst = Ext.create('Bleext.abstract.GridForm',{
    			pageLoad:true,
    			formColumnCount:4,
    			gridTitle : '查询结果',
    			formTitle: '订单查询',
    		    loadGridUrl:BP+'ord/pageQueryTord',
    		    addUrl:BP+'ord/insertTord',
    			updateUrl:BP+ 'ord/updateTord',
    			removeUrl:BP+'ord/removeTord',
    		    
    			columnConfig: [  {header: 'ID', dataIndex: 'id', hideGrid: true, hideForm: 'add', hidden:true ,readOnly: true},
    			                 			 {header:'订单号',dataIndex:'orderCode',hideForm:'add',width:300,readOnly:true},
    			                 			 {header: '日期',dataIndex: 'createTime',hideForm:'all',width:300,remoteSort:true,sortable:true},
		    					             {header: '经销商编号', dataIndex: 'distributorCode', allowBlank:false , fieldtype:'distListcombo',width:300,
			    				                   hiddenName:'distributorCode',listeners:{
				    				                    'beforequery' : function(e) {
				    				            			//e.combo.store.baseParams.distributorCode = e.query; //查询distributor编号
				    				                    	//TODO:e.combo.getStore( )
				    				                	},
				    				                	 'select':function(combo,records,index){
				    				                		 var form = this.up('form').getForm();
				    				                		 form.findField('distributorName').setValue(records[0].data.distributorName);
				    				                		 form.findField('shopCode').setValue(records[0].data.shop_Code);
				    				                		 form.findField('shopName').setValue(records[0].data.shop_Name);
				    						             }
		    					             }},
		    					            {header: '经销商名称', dataIndex: 'distributorName',readOnly:true, allowBlank:false,emptyText:'与编号联动',width:300},
		    					            {header: '店铺编号', dataIndex: 'shopCode',allowBlank:false,readOnly:true,emptyText:'与编号联动',remoteSort:true,sortable:true,width:300},
		    					            {header: '店铺名称', dataIndex: 'shopName',allowBlank:false,readOnly:true,emptyText:'与编号联动',width:300},
		    					            {header: '产品编号',  dataIndex: 'productCode',allowBlank:false,fieldtype:'prodListcombo',width:300,
		    						             hiddenName:'productCode',listeners:{
		    						            	 'beforequery' : function(e) {
		    						            			//e.combo.store.baseParams.productCode = e.query;// 查询product编号
		    						            		 	//TODO:e.combo.getStore( )
		    						                  },
		    					                	 'select':function(combo,records,index){
		    					                		 	var form = this.up('form').getForm();
		    							            		form.findField('productName').setValue(records[0].data.productName);
		    							            		form.findField('productPv').setValue(records[0].data.productPv);
		    							            		form.findField('productBv').setValue(records[0].data.productBv);
		    							            		form.findField('productPrice').setValue(records[0].data.productPrice);
		    							             }
		    					             }}, 
		    					            {header: '产品名称', dataIndex: 'productName',allowBlank:false,readOnly:true,emptyText:'与编号联动',width:300},
		    					            {header: '产品价格',dataIndex: 'productPrice', renderer: MoneyRender,emptyText:'与编号联动',allowBlank:false,readOnly:true,width:300},
		    					            {header: 'PV',dataIndex: 'productPv',readOnly:true,allowBlank:false,renderer: MoneyRender,emptyText:'与编号联动',width:300},
		    					            {header: 'BV',dataIndex: 'productBv',readOnly:true,allowBlank:false,renderer: MoneyRender,emptyText:'与编号联动',width:300},
		    					            {header: '销售数量', dataIndex: 'saleNumber',allowBlank:false ,vtype:'bank',width:300,
		    					              listeners:{
		    				                	 'keyup':function(_field , e){
		    				                		 	 var form = this.up('form').getForm();
			    				                		 var price_field = form.findField('productPrice');	//产品价格
			    				                		 var sumprice_field = form.findField('sumPrice');	//总价格
			    				                		 if(!Ext.isEmpty(price_field.getValue() && !Ext.isEmpty(_field.getValue() ))){	//总价格 = 产品价格 * 销售总量
			    				                			 sumprice_field.setValue(price_field.getValue() * _field.getValue() );
			    				                		 }else{	
			    				                			 sumprice_field.setValue(0.00);
			    				                		 }
		    				                	 }
		    					             },enableKeyEvents:true},
		    					            {header: '销售总额', dataIndex: 'sumPrice' ,allowBlank:false,readOnly:true ,renderer: MoneyRender,width:300},
		    					            {header: 'book', dataIndex: 'book' ,allowBlank:false ,vtype:'bank',width:300}],
    					            
 			    /*查询条件 : 订单号，经销商编号，产品编号，时间范围*/
    			formConfig:[ {fieldLabel: '经销商编号',xtype:'distListcombo',name:'distributorCode',width:300,
    									 hiddenName:'distributorCode',listeners:{
							                    'beforequery' : function(e) {
							            			//e.combo.store.baseParams.distributorCode = e.query; //查询distributor编号
							                	},
							                	 'select':function(combo,records,index){
							                		var form = this.up('form').getForm();
							                		form.findField("distributorName").setValue(records[0].data.distributorName);
									             }
							            }},
							            {xtype:'textfield',fieldLabel: '经销商名称', name: 'distributorName',readOnly:true,emptyText:'与编号联动',width:200},
							            {fieldLabel: '店铺编号', xtype:'shopListcombo',name:'shopCode',width:300,
								             hiddenName:'shopCode',listeners:{
								            	'select':function(combo,records,index){
								            		 var form = this.up('form').getForm();
								            		 form.findField('shopName').setValue(records[0].data.shopName);
								            	}
								         }},
								        {xtype:'textfield',fieldLabel: '店铺名称', name: 'shopName',readOnly:true,emptyText:'与编号联动',width:200},
								        {fieldLabel: '产品编号', xtype:'prodListcombo',name:'productCode',width:300,
							             hiddenName:'productCode',listeners:{
							            	 'beforequery' : function(e) {
							            			//e.combo.store.baseParams.productCode = e.query;// 查询product编号
							                	},
						                	 'select':function(combo,records,index){
						                		 	var form = this.up('form').getForm();
								            		form.findField('productName').setValue(records[0].data.productName);
								            }
							            }}, 
							            {xtype:'textfield',	fieldLabel: '产品名称', name: 'productName',readOnly:true,emptyText:'与编号联动',width:200},
						                {xtype:'datefield', format: 'Y-m-d H:i:s', editable: false, fieldLabel: '开始日期', name: 'startTime',width:250},
						                {xtype:'datefield', format: 'Y-m-d H:i:s', editable: false, fieldLabel: '结束日期', name: 'endTime' ,width:250},
						                {xtype:'textfield',	fieldLabel: 'book', name: 'book',width:300} ]
				    		});
    		return ordInst;
    }
});
